<?php
class UserAction extends CommonAction{
	
	public function _initialize() {
		header("Content-Type:text/html; charset=utf-8");
		$this->_inject_check(0);//调用过滤函数
		$this->_Config_name();//调用参数
		$this->_checkUser();
		$this->check_us_gq();
	}
	
	public function cody(){
		//===================================二级验证
		$UrlID = (int) $_GET['c_id'];
		if (empty($UrlID)){
			$this->error('二级密码错误!');
			exit;
		}
		if(!empty($_SESSION['user_pwd2'])){
			$url = __URL__."/codys/Urlsz/$UrlID";
			$this->_boxx($url);
			exit;
		}
		$cody   =  M ('cody');
		$list	=  $cody->where("c_id=$UrlID")->field('c_id')->find();
		if ($list){
			$this->assign('vo',$list);
			$this->display('Public:cody');
			exit;
		}else{
			$this->error('二级密码错误!');
			exit;
		}
	}
	public function codys(){
		//=============================二级验证后调转页面
		$Urlsz = (int) $_POST['Urlsz'];
		if(empty($_SESSION['user_pwd2'])){
			$pass  = $_POST['oldpassword'];
			$fck   =  M ('fck');
			if (!$fck->autoCheckToken($_POST)){
				$this->error('页面过期请刷新页面!');
				exit();
			}
			if (empty($pass)){
				$this->error('二级密码错误!');
				exit();
			}
	
			$where = array();
			$where['id'] = $_SESSION[C('USER_AUTH_KEY')];
			$where['passopen'] = md5($pass);
			$list = $fck->where($where)->field('id,is_agent')->find();
			if($list == false){
				$this->error('二级密码错误!');
				exit();
			}
			$_SESSION['user_pwd2'] = 1;
		}else{
			$Urlsz = $_GET['Urlsz'];
		}
		switch ($Urlsz){
			case 1;
			$_SESSION['Urlszpass'] = 'MyssHuoLongGuo';
			$bUrl = __URL__.'/relations';
			$this->_boxx($bUrl);
			break;
			case 2;
			$_SESSION['Urlszpass'] = 'Myssmemberx';
			$bUrl = __URL__.'/member_x';
			$this->_boxx($bUrl);
			break;
			case 3;
			$_SESSION['Urlszpass'] = 'Myssmemberz';
			$bUrl = __URL__.'/member_z';
			$this->_boxx($bUrl);
			break;
			case 10;
			$_SESSION['UrlPTPass'] = 'admintongji';
			$bUrl = __URL__.'/adminalltongji';
			$this->_boxx($bUrl);
			break;
			case 4;
			$_SESSION['UrlPTPass'] = 'MyssShuiPuTao';
			$bUrl = __URL__.'/user_info';
			$this->_boxx($bUrl);
			break;
			
			default;
                $bUrl = __APP__;
                $this->_box(1,'二级密码已验证，无需重复操作！',$bUrl,1);
			exit;
		}
	}
	public function user_address(){
		$fck    = M ('fck');
        $id=$_SESSION[C('USER_AUTH_KEY')];
        $rs=$fck->where('id='.$id)->find();
        $this->assign('rs',$rs);
     
		$this->display();

	} 
	public function user_card(){
		$fck    = M ('fck');
        $id=$_SESSION[C('USER_AUTH_KEY')];
        $rs=$fck->where('id='.$id)->find();
        $this->assign('rs',$rs);
		$this->display();

	} 
	public function user_zhuce(){
		$fck    = M ('fck'); 
        $id=$_SESSION[C('USER_AUTH_KEY')];
        $rs=$fck->where('id='.$id)->find();
        $this->assign('rs',$rs);
		$this->display();
	}
	
	
	public function usersAdd() {
		
		$fck    = M ('fck');  //注册表
		$id=$_SESSION[C('USER_AUTH_KEY')]; 
		
		
		$errmsg="";
	
		$huhu=trim($_POST['UserName']);
		if(empty($huhu)){
			$errmsg.="<li>请填写姓名！</li>";
		}
		
		if(empty($_POST['UserTel'])){
			$errmsg.="<li>请填写电话号码！</li>";
		}
		if(!preg_match("/^1[34578]\d{9}$/",$_POST['UserTel'])){
			$this->error('电话号码格式不正确！');
			exit;
}
       $rs  = $fck->where('id='.$id)->find();
    if($rs){

		   $data['user_name']           = $_POST['UserName'];             //姓名          
		   $data['user_tel']            = $_POST['UserTel'];              //联系电话
		
		                    //注册等级
		   $result = $fck->where('id='.$id)->save($data);

		  if($result) {
			
			
			$url = __URL__.'/user_info';
			$this->_box(0,'保存成功！',$url,1);
			exit;
		}

       }
		
elseif(!$rs){
		//登录次数
		$data['user_name']           = $_POST['UserName'];             //姓名          
		$data['user_tel']            = $_POST['UserTel'];              //联系电话
		
		                    //注册等级
		$result = $fck->add($data);

		if($result){
			
			
			$url = __URL__.'/user_info';
			$this->_box(0,'保存成功！',$url,1);
			exit;
		}
	} 
	else{
			$this->error('会员注册失败！');
			exit;
		}
	}

	//退出本金申请
	public function user_exit(){
		$fck=M ('fck');
		$fee=M('fee');
		$id=$_SESSION[C('USER_AUTH_KEY')]; 
		$user=$fck->where('id='.$id)->find();
		$fee_rs = $fee->field('s7,s12')->find();
		$month=explode('|',$fee_rs['s12']);//月份等级
		$money=explode('|',$fee_rs['s7']);//金额百分比
		$pay_time=$user['pdt'];
		//2592000 一个月平均秒数
		$in_time=time();
		$c_time=$in_time-$pay_time;//开通到当前的时间秒数
		$month_num=floor($c_time/2592000);//计算月数


		//计算月份等级
		$month_level=0;
		$max_level=count($month);
		foreach ($month as $key => $value) {
			if($month_num>$value && $month_num<$month[$key+1]){
				$month_level=$key+1;
			}
			if($month_num>$month[$max_level-1]){
				$month_level=$max_level;
			}
		}
		//用等级取金额百分比
		$money_bfb=$money[$month_level-1];

		$list=$user;
		$list['month_num']=$month_num;
		$list['money_bfb']=$money_bfb;
		$list['month_level']=$month_level;
		$list['exit_money']=$list['cpzj']*$money_bfb/100;
		$this->assign('list',$list);
		$this->display();
	}

	//退出本金申请处理
	public function user_exitAC(){
		$fck=M ('fck');
		$fee=M('fee');
		$id=$_SESSION[C('USER_AUTH_KEY')]; 
		$user=$fck->where('id='.$id)->find();
		$fee_rs = $fee->field('s7,s12')->find();
		$month=explode('|',$fee_rs['s12']);//月份等级
		$money=explode('|',$fee_rs['s7']);//金额百分比
		$pay_time=$user['pdt'];
		//2592000 一个月平均秒数
		$in_time=time();
		$c_time=$in_time-$pay_time;//开通到当前的时间秒数
		$month_num=floor($c_time/2592000);//计算月数


		//计算月份等级
		$month_level=0;
		$max_level=count($month);
		foreach ($month as $key => $value) {
			if($month_num>$value && $month_num<$month[$key+1]){
				$month_level=$key+1;
			}
			if($month_num>$month[$max_level-1]){
				$month_level=$max_level;
			}
		}

		//用等级取金额百分比
		$money_bfb=$money[$month_level-1];

		if($month_level==0){
			$this->error('您未满足申请日期！');
			exit;
		}

		if($user['is_fenh']==1){
			$this->error('您已经申请过了！');
			exit;
		}

		$exit_money=$user['cpzj']*$money_bfb/100;
		$res =  M('fck')->where('id='.$id)->execute('update __TABLE__ set is_fenh=1 where id='.$id);
		A('Common')->zijin_log($id,$user['user_id'],"exit_money","退出本金",$exit_money,"agent_use");
		$url=__URL__.'/user_exit';
		$this->_box(0,'退出本金成功！',$url,1);
		exit;
	}

	public function usercard(){
		$id=$_SESSION[C('USER_AUTH_KEY')]; 
		$errmsg="";
        $user_name=trim($_POST['user_name']);
        $name=trim($_POST['name']);
        $user_id=trim($_POST['user_id']);
        if(empty($user_id)){
			$errmsg.="<li>请填写帐号！</li>";
		}
		if(empty($user_name)){
			$errmsg.="<li>请填写银行开户真实姓名！</li>";
		}
		if(empty($_POST['user_tel'])){
			$errmsg.="<li>请填写电话号码！</li>";
		}
		if(!preg_match("/^1[3456789]\d{9}$/",$_POST['user_tel'])){
			$this->error('电话号码格式不正确！');
			exit;
        }
        $s_err = "<ul>";
        $e_err = "</ul>";
        if(!empty($errmsg)){
            $out_err = $s_err.$errmsg.$e_err;
            $this->error($out_err);
            exit;
        }
        $data = array();
        $data['user_id']        = trim($_POST['user_id']);                 
       	$data['bank_card']      = trim($_POST['bank_card']);                       
        $data['user_tel']       = trim($_POST['user_tel']);
        $data['name']       	= trim($_POST['name']);
        $data['user_name']      = trim($_POST['user_name']);
        $data['bank_name']      = trim($_POST['bank_name']);
        $data['account']        = trim($_POST['account']);
		$result = M('fck')->where('id='.$id)->save($data);
		if($result) {
            M('fck')->where('id='.$id)->setInc('down_num',1);
			$url = __URL__.'/user_info';
			$this->success('保存成功！',$url);
			exit;
		}else{
             $this->error('资料未修改或修改失败！');
             exit;
        }
	}
	public function useradress(){
		$fck=M('fck');
        $id=$_SESSION[C('USER_AUTH_KEY')];

        $errmsg="";
        $huhu=trim($_POST['receive_name']);

		if(empty($huhu)){
			$errmsg.="<li>请填写收货姓名！</li>";
		}
		
		if(empty($_POST['receive_tel'])){
			$errmsg.="<li>请填写电话号码！</li>";
		}
        if(empty($_POST['receive_address'])){
            $errmsg.="<li>请填写详细收货地址！</li>";
        }
		if(!preg_match("/^1[34578]\d{9}$/",$_POST['receive_tel'])){
			$this->error('电话号码格式不正确！');
			exit;
        }

        $s_err = "<ul>";
        $e_err = "</ul>";
        if(!empty($errmsg)){
            $out_err = $s_err.$errmsg.$e_err;
            $this->error($out_err);
            exit;
        }
        $data=array();
		$data['receive_name']        = trim($_POST['receive_name']);                        //最后登录时间
		$data['receive_address']     = trim($_POST['receive_address']);                        //最后登录时间
		$data['receive_tel']         = trim($_POST['receive_tel']);                        //最后登录时间
        $result = $fck->where('id='.$id)->save($data);
        if($result) {
			$url = __URL__.'/user_info';
			$this->_box(0,'保存成功！',$url,1);
			exit;
		}else{
			$this->error('资料未修改或保存失败！');
			exit;
		}
	}

	public function look_big(){
	    $id = I('id');
        $img = M('fck')->where("id={$id}")->getField('weixinCode');
        $this->assign('img',$img);
        $this->display();
    }

	//推荐表
	public function relations($Urlsz=0){
		//推荐关系
		if ($_SESSION['Urlszpass'] == 'MyssHuoLongGuo'){
			$fck = M('fck');
			$UserID = $_REQUEST['UserID'];
			if (!empty($UserID)){
				$map['user_id'] = array('like',"%".$UserID."%");
			}
			$map['re_id'] = $_SESSION[C('USER_AUTH_KEY')];
			$map['is_pay'] = 1;

            $field  = '*';
            //=====================分页开始==============================================
            import ( "@.ORG.ZQPage" );  //导入分页类
            $count = $fck->where($map)->count();//总页数
    	    $listrows = C('ONE_PAGE_RE');//每页显示的记录数
			$page_where = 'UserID='.$UserID;//分页条件
            $Page = new ZQPage($count, $listrows, 1, 0, 3, $page_where);
            //===============(总页数,每页显示记录数,css样式 0-9)
            $show = $Page->show();//分页变量
            $this->assign('page',$show);//分页变量输出到模板
            $list = $fck->where($map)->field($field)->order('pdt desc')->page($Page->getPage().','.$listrows)->select();
            $HYJJ = '';
            $this->_levelConfirm($HYJJ,1);
            $this->assign('voo',$HYJJ);//会员级别
            $this->assign('list',$list);//数据输出到模板
            //=================================================

			$this->display ('relations');
			return;
		}else{
			$this->error('数据错误2!');
			exit;
		}
	}
	
	//前后5人
	public function member_x(){
		if ($_SESSION['Urlszpass'] == 'Myssmemberx'){
			$fck = M('fck');
			$id = $_SESSION[C('USER_AUTH_KEY')];
			$myrs = $fck->where('id='.$id)->field('id,user_id,n_pai')->find();
			$n_pai = $myrs['n_pai'];
			
			$field  = 'id,user_id,n_pai,pdt,user_tel,qq';
			//前面5个
    		$wherea = "is_pay>0 and n_pai<".$n_pai;
            $alist = $fck->where($wherea)->field($field)->order('n_pai desc')->limit(5)->select();
            $this->assign('alist',$alist);
            
            //后5个
    		$whereb = "is_pay>0 and n_pai>".$n_pai;
            $blist = $fck->where($whereb)->field($field)->order('n_pai asc')->limit(5)->select();
            $this->assign('blist',$blist);
//            dump($blist);exit;

			$this->display ('member_x');
			return;
		}else{
			$this->error('数据错误!');
			exit;
		}
	}
	
	//一线排网
	public function member_z(){
		if ($_SESSION['Urlszpass'] == 'Myssmemberz'){
			$fck = M('fck');
			$id = $_SESSION[C('USER_AUTH_KEY')];
			$myrs = $fck->where('id='.$id)->field('id,user_id,x_pai')->find();
			$x_pai = $myrs['x_pai'];
			
			$field  = 'id,user_id,x_pai,pdt,user_tel,qq,x_num,x_out';

    		$wherea = "is_pay>0 and x_pai>=".$x_pai;
    		//=====================分页开始==============================================
            import ( "@.ORG.ZQPage" );  //导入分页类
            $count = $fck->where($wherea)->count();//总页数
       		$listrows = 20;//每页显示的记录数
            $page_where = '';//分页条件
            $Page = new ZQPage($count, $listrows, 1, 0, 3, $page_where);
            //===============(总页数,每页显示记录数,css样式 0-9)
            $show = $Page->show();//分页变量
            $this->assign('page',$show);//分页变量输出到模板
            $list = $fck->where($wherea)->field($field)->order('x_pai asc,id asc')->page($Page->getPage().','.$listrows)->select();
            $this->assign('list',$list);//数据输出到模板
            //=================================================
            
            $nn = $fck->where("is_pay>0 and x_pai<".$x_pai." and x_out=0")->count();
            $this->assign('nn',$nn);

			$this->display ('member_z');
			return;
		}else{
			$this->error('数据错误!');
			exit;
		}
	}
	
	//统计
	public function adminalltongji(){
		$this->_Admin_checkUser();
		$fck = M ('fck');
		$msg = M ('msg');
		$chongzhi = M ('chongzhi');
		$tiqu = M ('tiqu');
		$tiqu = M ('tiqu');
		
		$now_day = strtotime(date("Y-m-d"));
		$end_day = $now_day+3600*24;
		
		$yes_day = $now_day-3600*24;
		
		$yes_c = $fck->where('is_pay>0 and pdt>='.$yes_day.' and pdt<'.$now_day)->count();//昨日新进
		$day_c = $fck->where('is_pay>0 and pdt>='.$now_day.' and pdt<'.$end_day)->count();//今日新进
		$not_c = $fck->where('is_pay=0')->count();//未开通
		$msg_c = $msg->where("s_uid=1 and s_read=0")->count(); //总记录数
		$chz_c = $chongzhi->where("is_pay=0")->count(); //充值
		$tix_c = $tiqu->where("is_pay=0")->count(); //提现
		$bad_c = $fck->where("is_agent=1 and is_pay>0")->count(); //报单中心
		
		$this->assign('yes_c',$yes_c);
		$this->assign('day_c',$day_c);
		$this->assign('not_c',$not_c);
		$this->assign('msg_c',$msg_c);
		$this->assign('chz_c',$chz_c);
		$this->assign('tix_c',$tix_c);
		$this->assign('upl_c',0);
		$this->assign('bad_c',$bad_c);
		$this->assign('did_c',0);
		
		$this->display();
	}
	
	
	//未开通会员
	public function user_info(){
		//列表过滤器，生成查询Map对象
		
		$this->display();
	}


    /**
     * 复投
     */
    public function becomeMember(){
        $fck_rs = M('fck')->where("id={$_SESSION[C('USER_AUTH_KEY')]}")->field('chuju,u_level,agent_use')->find();
        $this->assign('fck_rs',$fck_rs);
        $HYJJ="";
        $this->_levelConfirm($HYJJ,1);
        $this->assign('voo',$HYJJ);//会员级别

        $fee_rs = M('fee')->field('s9,s10')->find(1);
        $s9 = explode('|',$fee_rs['s9']);
        $s10 = explode('|',$fee_rs['s10']);

        $this->assign('s9',$s9);
        $this->assign('s10',$s10);

        $this->display();
    }

    /**
     * 复投结算
     */
    public function becomeMemberSave(){
        $id = $_SESSION[C('USER_AUTH_KEY')];
        $fck_rs = M('fck')->where("id={$id}")->field('u_level,agent_use,chuju')->find();

        if ($fck_rs['chuju'] == 0){
            $this->error('账户未出局！');
        }

        $u_level = I('class');
        if ($u_level < $fck_rs['u_level']){
            $this->error('请选择等于或高于当前的级别！');
        }

        $fee_rs = M('fee')->field('s9,s10')->find(1);
        $s9 = explode('|',$fee_rs['s9']);

        if ($fck_rs['agent_use'] < $s9[$u_level-1]){
            $this->error('电子币余额不足！');
        }

        $res =  M('fck')->where('id='.$id)->execute('update __TABLE__ set agent_use=agent_use-'.$s9[$u_level-1].' where id='.$id);

        if ($res){
            //添加报单产品
            $this->baodanProduct($id,2);

            M('fck')->where('id='.$id)->setField(array('cpzj'=>$s9[$u_level-1],'u_level'=>$u_level,'chuju'=>0,'leiji'=>0));
            unset($fck_rs,$fee_rs);
            $url = __URL__.'/becomeMember/';
            $this->_box(1,'复投成功！',$url,1);
        }else{
            unset($fck_rs,$fee_rs);
            $url = __URL__.'/becomeMember/';
            $this->_box(1,'复投失败！',$url,1);
        }


    }

    public function baodan(){
        $map = array();
        $map['id'] = array('gt',0);
        $user_id = trim(I('UserID'));
        if (!empty($user_id)){
            $map['user_id'] = $user_id;
            $this->assign('UserID',$user_id);

        }

        $is_pay = trim(I('is_pay'));
        if (!empty($is_pay)  || $is_pay=='0'){

            $map['is_pay'] = $is_pay;
            $this->assign('is_pay',$is_pay);
        }
        $type = trim(I('type'));

        if (!empty($type) || $type=='0'){
            $map['type'] = $type;
            $this->assign('type',$type);
        }


        import ( "@.ORG.ZQPage" );  //导入分页类
        $count = M('baodan')->where($map)->count();//总页数
        $listrows = 20;
        $page_where = 'UserID='.$user_id;//分页条件
        $Page = new ZQPage($count, $listrows, 1, 0, 3, $page_where);
        //===============(总页数,每页显示记录数,css样式 0-9)
        $show = $Page->show();//分页变量
        $this->assign('page',$show);//分页变量输出到模板
        $list = M('baodan')->where($map)->order('id desc')->page($Page->getPage().','.$listrows)->select();
        $HYJJ = '';
        $this->_levelConfirm($HYJJ,1);
        $this->assign('voo',$HYJJ);//会员级别
        $this->assign('list',$list);//数据输出到模板
        $this->display();
    }

    public function baodanSave(){
        //处理提交按钮
        $action = $_POST['action'];
        //获取复选框的值
        $XGid = $_POST['tabledb'];
        if (!isset($XGid) || empty($XGid)){
            $bUrl = __URL__.'/baodan';
            $this->_box(0,'请选择！',$bUrl,1);
            exit;
        }
        switch ($action){
            case '处理';
                $this->baodanchuli($XGid);
                break;
            case '删除';
                $this->baodanDel($XGid);
                break;
            default;
                $bUrl = __URL__.'/baodan';
                $this->_box(0,'未找到该记录！',$bUrl,1);
                break;
        }
    }

    private function baodanchuli($XGid){


            $where = array();
            $where['id'] = array ('in',$XGid);
            $where['is_pay'] = array ('eq',0);

            $valuearray = array(
                'is_pay' => '1',
            );
            $res = M('baodan')->where($where)->setField($valuearray);
            unset($shopping,$where);

            if ($res){
                $bUrl = __URL__.'/baodan';
                $this->_box(1,'处理成功！',$bUrl,1);
            }else{
                $bUrl = __URL__.'/baodan';
                $this->_box(1,'处理失败！',$bUrl,1);
            }
    }

    private function baodanDel($XGid){


            $where = array();
            $where['id'] = array ('in',$XGid);
            $res = M('baodan')->where($where)->delete();

        if ($res) {
            $bUrl = __URL__ . '/baodan';
            $this->_box(1, '删除成功！', $bUrl, 1);
        }else{
            $bUrl = __URL__.'/baodan';
            $this->_box(1,'删除失败！',$bUrl,1);
            exit;
        }


    }
    //导出报单表格
    public function daochuTable(){

        //导出excel
        set_time_limit(0);

        header("Content-Type:   application/vnd.ms-excel");
        header("Content-Disposition:   attachment;   filename=报单信息.xls");
        header("Pragma:   no-cache");
        header("Content-Type:text/html; charset=utf-8");
        header("Expires:   0");



        $baodan = M ('baodan');  //奖金表


        $map = array();
        $map['id'] = array('gt',0);
        $field   = '*';
        $list = $baodan->where($map)->field($field)->order('id desc')->select();

        $title   =   "报单信息表 导出时间:".date("Y-m-d   H:i:s");

        echo   '<table   border="1"   cellspacing="2"   cellpadding="2"   width="50%"   align="center">';
        //   输出标题
        echo   '<tr   bgcolor="#cccccc"><td   colspan="10"   align="center">'   .   $title   .   '</td></tr>';
        //   输出字段名
        echo   '<tr  align=center>';
        echo   "<td>序号</td>";
        echo   "<td>会员编号</td>";
        echo   "<td>类型</td>";
        echo   "<td>级别</td>";
        echo   "<td>注册资金</td>";
        echo   "<td>创建时间</td>";
        echo   "<td>姓名</td>";
        echo   "<td>收货电话</td>";
        echo   "<td>收货地址</td>";
        echo   "<td>状态</td>";
        echo   '</tr>';


        $i = 0;
        foreach($list as $row)   {
            $i++;
            $num = strlen($i);
            if ($num == 1){
                $num = '000'.$i;
            }elseif ($num == 2){
                $num = '00'.$i;
            }elseif ($num == 3){
                $num = '0'.$i;
            }else{
                $num = $i;
            }
            if ($row['is_pay'] ==0){
                $is_pay= "未处理";
            }else{
                $is_pay= "已处理";
            }
            if ($row['type'] ==0 ){
                $type = "后台开通";
            }
            if ($row['type'] ==1 ){
                $type = "代理开通";
            }
            if ($row['type'] ==2 ){
                $type = "账号复投";
            }

            $HYJJ = '';
            $this->_levelConfirm($HYJJ,1);
            $class = $HYJJ[$row['u_level']];
            echo   '<tr align=center>';
            echo   "<td>"   .   $num.  "</td>";
            echo   "<td>"   .   $row['user_id'].  "</td>";
            echo   "<td>"   .   $type.  "</td>";
            echo   "<td>"   .   $class.  "</td>";
            echo   "<td>"   .   $row['cpzj'].  "</td>";
            echo   "<td>"   .   date("Y-m-d H:i:s",$row['pdt']).  "</td>";
            echo   "<td>"   .   $row['receive_name'].  "</td>";
            echo   "<td>"   .   $row['receive_tel'].  "&nbsp;</td>";
            echo   "<td>"   .   $row['receive_address'].  "&nbsp;</td>";
            echo   "<td>"   .   $is_pay.  "</td>";

            echo   '</tr>';
        }
        echo   '</table>';
    }

    //五轨大公排
    public function gongpaixtsmall_five($uid){
        $fck = M ('fck');
        $mouid=$uid;
        $field = 'id,user_id,p_level,p_path,u_pai';
        $where = 'is_pay>0 and (p_path like "%,'.$mouid.',%" or id='.$mouid.')';

        $re_rs = $fck ->where($where)->order('p_level asc,u_pai+0 asc')->field($field)->select();
        $fck_where = array();
        foreach($re_rs as $vo){
            $faid=$vo['id'];
            $fck_where['is_pay']   = array('egt',0);
            $fck_where['father_id']   = $faid;
            $count = $fck->where($fck_where)->count();
            if ( is_numeric($count) == false){
                $count = 0;
            }
            if ($count<5){
                $father_id=$vo['id'];
                $father_name=$vo['user_id'];
                $TreePlace=$count;
                $p_level=$vo['p_level']+1;
                $p_path=$vo['p_path'].$vo['id'].',';
                $u_pai=$vo['u_pai']*5+$TreePlace-3;
                $arry=array();
                $arry['father_id']=$father_id;
                $arry['father_name']=$father_name;
                $arry['treeplace']=$TreePlace;
                $arry['p_level']=$p_level;
                $arry['p_path']=$p_path;
                $arry['u_pai']=$u_pai;
                return $arry;
                break;
            }
        }
    }
    /**
     * 上传图片
     * **/
    public function upload_fengcai_pp() {
        if(!empty($_FILES)) {
            //如果有文件上传 上传附件
            $this->_upload_fengcai_pp();
        }
    }

    protected function _upload_fengcai_pp()
    {
        header("content-type:text/html;charset=utf-8");
        // 文件上传处理函数

        //载入文件上传类
        import("@.ORG.UploadFile");
        $upload = new UploadFile();

        //设置上传文件大小
        $upload->maxSize  = 1048576 * 2 ;// TODO 50M   3M 3292200 1M 1048576

        //设置上传文件类型
        $upload->allowExts  = explode(',','jpg,gif,png,jpeg');

        //设置附件上传目录
        $upload->savePath =  './Public/Uploads/image/weixinCode/';

        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb =  false;

        //设置需要生成缩略图的文件前缀
        $upload->thumbPrefix   =  'm_';  //生产2张缩略图

        //设置缩略图最大宽度
        $upload->thumbMaxWidth =  '800';

        //设置缩略图最大高度
        $upload->thumbMaxHeight = '600';

        $uid = I('uid');

        //设置上传文件规则
//        $upload->saveRule = $uid;
        $upload->saveRule = date("Y").date("m").date("d").date("H").date("i").date("s").rand(1,100);

        //删除原图
        $upload->thumbRemoveOrigin = true;

        //存在同名文件是否是覆盖
        $upload->uploadReplace = true;


        if(!$upload->upload()) {
            //捕获上传异常
            $error_p=$upload->getErrorMsg();
            echo "<script>alert('".$error_p."');history.back();</script>";
        }else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            $U_path=$uploadList[0]['savepath'];
            $U_nname=$uploadList[0]['savename'];
            $U_inpath=(str_replace('./Public/','__PUBLIC__/',$U_path)).$U_nname;

            echo "<script>window.parent.form1.image1.value='".$U_inpath."';</script>";
            echo "<span style='font-size:12px;'>上传完成！</span>";
            exit;

        }
    }

    public function upload_fengcai_bb() {
        if(!empty($_FILES)) {
            //如果有文件上传 上传附件
            $this->_upload_fengcai_bb();
        }
    }

    protected function _upload_fengcai_bb()
    {
        header("content-type:text/html;charset=utf-8");
        // 文件上传处理函数

        //载入文件上传类
        import("@.ORG.UploadFile");
        $upload = new UploadFile();

        //设置上传文件大小
        $upload->maxSize  = 1048576 * 2 ;// TODO 50M   3M 3292200 1M 1048576

        //设置上传文件类型
        $upload->allowExts  = explode(',','jpg,gif,png,jpeg');

        //设置附件上传目录
        $upload->savePath =  './Public/Uploads/image/bank/';

        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb =  false;

        //设置需要生成缩略图的文件前缀
        $upload->thumbPrefix   =  'm_';  //生产2张缩略图

        //设置缩略图最大宽度
        $upload->thumbMaxWidth =  '800';

        //设置缩略图最大高度
        $upload->thumbMaxHeight = '600';

        $uid = I('uid');

        //设置上传文件规则
//        $upload->saveRule = $uid;
        $upload->saveRule = date("Y").date("m").date("d").date("H").date("i").date("s").rand(1,100);

        //删除原图
        $upload->thumbRemoveOrigin = true;

        //存在同名文件是否是覆盖
        $upload->uploadReplace = true;


        if(!$upload->upload()) {
            //捕获上传异常
            $error_p=$upload->getErrorMsg();
            echo "<script>alert('".$error_p."');history.back();</script>";
        }else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            $U_path=$uploadList[0]['savepath'];
            $U_nname=$uploadList[0]['savename'];
            $U_inpath=(str_replace('./Public/','__PUBLIC__/',$U_path)).$U_nname;

            echo "<script>window.parent.form1.image1.value='".$U_inpath."';</script>";
            echo "<span style='font-size:12px;'>上传完成！</span>";
            exit;

        }
    }

    /**
     *成为正式会员的确认金额
     */
    public function applyForMember() {
        $money = $this->getFeeRule(['money_tobe_service'])['money_tobe_service']['0'];
        $this->assign('money', $money);

        if(isset($_POST['money'])) {
            $id = $_SESSION[C('USER_AUTH_KEY')];
            $rs = M('fck')->where(['id' => $id, 'is_pay' => 0, 'is_agent' => 0])->setField('cpzj', $money);

            if($rs) {
                $_SESSION['first_recharge'] = $money;
                $bUrl = U('Recharge/cody', ['c_id' => 1, 'first_recharge' => $money]);
                $this->_box(1,'提交成功，转到充值页面！ 等待审核通过',$bUrl,2);
                exit;
                //$this->success('提交成功，等待审核通过');
            } else {
                $this->error('提交失败，确认是否已充值 ,可能已提交过 或者已申请服务中心，可联系服务人员');
            }
        }
        $this->display();
    }

    /**
     * 会员激活
     * 1.判断会员级别的金额，用掉相应积分
     *
     * @return bool
     */
    public function userActive() {
        $fee = $this->getFeeRule(['money_tobe_service'])['money_tobe_service'];     //会员级别的金额

        $fee_rs = $this->getFeeRule();
        $this->assign('fee', $fee_rs);

        $money = 0;
        $id = $_SESSION[C('USER_AUTH_KEY')];
        $fck = D('Fck');
        $rs = $fck->where(['id' => $id, 'is_pay' => 0])->find();      //is_aa是已扣掉
        $fck_rs = $fck->where(['id' => $id])->find();      //is_aa是已扣掉
        $this->assign('fck_rs', $fck_rs);


        if($rs['is_agent'] == 0 && $rs['shop_a'] == '') {
            $money = $fee['0'];
        } else if($rs['is_agent'] == 1 && $rs['shoplx'] == '1') {
            $money = $fee['2'];
            //市级运营中心
        } else if($rs['is_agent'] == 1 && $rs['shoplx'] == '2') {
            $money = $fee['1'];
            //区,县运营中心
        }

        if(isset($_POST['money'])) {
            if(!$rs)  {
                $this->error('没有待激活会员');
                return false;
            }

            //开始激活充值后的会员，记录
            $data['b1'] = 0;
            $data['is_fenh'] = 0;
            $data['pdt'] = time();
            //$data['agent_use'] = $rs['agent_use'] - $money;   不用扣费
            $data['is_aa'] = 1;  //是否已经扣钱

            //会员激活
            if($rs['is_agent'] == 0 && $rs['shop_a'] == '' && $rs['is_pay'] == 0) {
                //$data['is_pay'] = 1;
                $result = $fck->where('id='.$id)->save($data);  //开通会员，保存
                //$fck->addencAdd($id,$id, -$money, 60,'会员激活','user_active',0);

            } elseif ($rs['is_agent'] == 1 && $rs['shoplx'] == '1' && $rs['is_aa'] == 0) {      //市级运营中心
                $result = $fck->where('id='.$id)->save($data);  //激活市级，扣钱，保存
                $money = $fee['2'];
                //$fck->addencAdd($id,$id, -$money, 61,'市级中心激活','agent_city_active',0);
            } elseif ($rs['is_agent'] == 1 && $rs['shoplx'] == '2' && $rs['is_aa'] == 0) {  //区,县运营中心
                $result = $fck->where('id='.$id)->save($data);
                $money = $fee['1'];
                //$fck->addencAdd($id,$id, -$money, 62,'体验中心县/区激活','agent_city_active',0);
            }

            $bUrl = U('Index/index');
            $this->_box(1,'激活成功，等待审核通过',$bUrl,1);
            exit;
        }

        $this->assign('money', $money);

        if(isset($_POST['image'])) {    //图片
            if(!$fck_rs)  {
                $this->error('没有待激活会员');
                return false;
            }
            M('fck')->where(['id' => $id])->save(['lang' => $_POST['image']]);
            $bUrl = U('User/userActive');
            $this->_box(1,'已提交凭证',$bUrl,1);
            exit;
        }

        $this->display();
    }

}
?>